// pages/shiye/components/inputTextarea/inputTextArea.ts


Component({
  /**
   * 组件的属性列表
   */
  properties: {
    forceEnableSend: {
      type: Boolean,
      value: false,
    },
    inputValue: {
      type: String,
      value: "",
      observer: function (newVal) {
        this.setData({
          question: newVal, // Update question when inputValue changes
        });
      },
    },
    isloading: {
      type: Boolean,
      value: false,
    },
    inputBgc: {
      type: String,
      value: "#fff",
    },
    minHeight: {
      type: String,
      value: "140rpx",
    },
    inputInnerPaddingV: {
      type: String,
      value: "20rpx",
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    question: "",
    isSpeaking: false,
    forceSend: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onValueChanged(value: string) {
      this.triggerEvent("onChanged", value || "");
    },
    bindTextAreaInput: function (e: any) {
      const newValue = e.detail.value;
      this.setData({
        question: newValue,
      });
      // Set the value again to ensure the cursor is at the end
      this.triggerEvent("onChanged", newValue);
    },
    initManager() {
      var plugin = requirePlugin("WechatSI");
      let manager = plugin.getRecordRecognitionManager();
      let that = this;
      manager.onStop = function (res) {
        // console.log("record file path", res.tempFilePath)
        // console.log("result", res.result)
        // this.setData({
        //   isSpeaking: false
        // })
        if (!res.result || res.result.length === 0) {
          wx.showToast({
            title: "没有听清，请重试~",
            icon: "none",
          });
          return;
        }
        that.setData({
          question: that.data.question + res.result || "",
        });
        that.onValueChanged(that.data.question);
      };
      manager.onStart = function (res) {
        // console.log("成功开始录音识别", res)
        // this.setData({
        //   isSpeaking: true
        // })
      };
      manager.onError = function (res) {
        console.error("error msg", res)
        if (res.retcode === -30001) {
          wx.showToast({
            title: " 没有识别到语音，请确认麦克风是否被打开",
            icon: "none",
          });

        } else {
          wx.showToast({
            title: "没有听清，请重试~",
            icon: "none",
          });
        }

        that.setData({
          isSpeaking: false,
        });
      };
      return manager
    },
    speakFn() {
      // console.log("speak", this.data.isSpeaking);
      if (this.properties.isloading) {
        wx.showToast({
          title: "正在生成中...",
          icon: "none",
        });
        return;
      }
      const manager = this.initManager()
      if (!this.data.isSpeaking) {
        manager.start({ duration: 60000, lang: "zh_CN" });
      } else {
        manager.stop();
      }
      this.setData({
        isSpeaking: !this.data.isSpeaking,
      });
    },
    sendFn() {
      // console.log("send");
      if (this.properties.isloading) return;
      if (this.properties.isSpeaking) {
        wx.showToast({
          title: "当前正在语音输入，请先停止",
          icon: "none",
        });
        return;
      }
      this.triggerEvent("send");
    },
  },
  lifetimes: {
    ready() {

      this.setData({
        forceSend: this.properties.forceEnableSend,
        question: this.properties.inputValue || "",
      });
      this.triggerEvent("onChanged", this.data.question);

    },
  },
});
